﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.SqlServer.Management.Smo;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;


namespace ConnectDatabase
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            btnOK.IsEnabled = false;
            cmbAuthen.SelectedIndex = 0;
            loadcmbServer();

        }
        private void loadcmbServer()
        {
            string[] serverNames = (string[])Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Microsoft SQL Server").GetValue("InstalledInstances");
            foreach (string serverName in serverNames)
            {
                cmbServer.Items.Add(serverName);
            }
        }
        private void cmbServer_DropDownOpened(object sender, EventArgs e)
        {

        }
        private void visiblewindow(bool vis)
        {
            lbUserName.IsEnabled = vis;
            lbPassword.IsEnabled = vis;
            txtPassword.IsEnabled = vis;
            txtUserName.IsEnabled = vis;

        }
        private void cmbAuthen_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbAuthen.SelectedIndex == 1)
            {
                visiblewindow(true);
            }
            else
                visiblewindow(false);
        }

        
        SqlConnection conn;
        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            conn = GetConnect(cmbAuthen.SelectedIndex);
            cmbDB.Items.Clear();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT name FROM SYSDATABASES", conn);
            DataTable tb = new DataTable();
            da.Fill(tb);
            foreach (var i in tb.DefaultView)
            {
                cmbDB.Items.Add(i);
            }
            
            cmbDB.DisplayMemberPath = "name";
            
            cmbDB.SelectedValuePath = "name";
            cmbDB.SelectedIndex = 0;
        }
        private SqlConnection GetConnect(int typeAuthen)
        {
            if (typeAuthen == 0)
            {
                return new SqlConnection("Data Source=" + cmbServer.Text.Trim() + ";Integrated Security=True;");
            }
            else
                return new SqlConnection("Data Source=" + cmbServer.Text.Trim() + ";Persist Security Info=True;User ID=" + txtUserName.Text.TrimEnd() + ";Password=" + txtPassword.Password + ";");
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            StreamWriter wr = new StreamWriter("C:\\ConnectionsString.txt");
            string cnstr = conn.ConnectionString + "Initial Catalog=" + cmbDB.SelectedValue;
            wr.Write(cnstr);
            wr.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cmbDB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnOK.IsEnabled = true;
        }
    }
}
        